package com.yanqu.road.server.cmd.pageant;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.game.result.UserPageantDataResult;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.logic.rank.RankListModel;
import com.yanqu.road.pb.pageant.PageantProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.game2048.PageantModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.GamePageantMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.rank.ActivityRankMgr;
import com.yanqu.road.server.pb.PageantPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

import java.util.List;

@Cmd(code = Protocol.S_PAGEANT_SETTLEMENT, desc = "结算选美大会活动")
public class PageantEndActivityCmd extends AbstractCommand {

    private int activityId;

    private UserPageantDataResult result;

    private boolean needSend = true;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        PageantProto.PageantSettlementBeautyReqMsg reqMsg = PageantProto.PageantSettlementBeautyReqMsg.parseFrom(packet.getBytes());
        activityId = reqMsg.getActivityId();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        ActivityInfo activityInfo = GamePageantMgr.getActivityInfo();
        if (!ActivityMgr.activityInShowTime(activityInfo)) {
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }
        List<ActivityConditionInfo> conditions = NormalActivityMgr.getActivityConditionInfoList(activityId);
        if (conditions != null && conditions.size() > 0) {
            if (conditions.get(0).getType() == eGamePlayerEventType.PageantSingleScoreRank.getValue() || conditions.get(0).getType() == eGamePlayerEventType.PageantTotalScoreRank.getValue()) {

            } else {
                needSend = false;
                // 发送到跨服获取排名
                PageantProto.CrossPageantEndReqMsg.Builder req = PageantProto.CrossPageantEndReqMsg.newBuilder();
                req.setActivityId(activityId);
                player.sendPacket(CrossProtocol.C2_PAGEANT_END, req);
                return;
            }
        }
        result = player.getModule(PageantModule.class).settlementBeautyActivity(activityId);
        RankListModel rankListModel = ActivityRankMgr.getActivityRankListModel(activityId,  eGamePlayerEventType.PageantSingleScoreRank.getValue());//单场
        if (null != rankListModel) {
            int ranking = rankListModel.unlockGetMyRank(player.getUserId());
            result.setSingleRanking(ranking);
        }

        RankListModel rankListModelAll = ActivityRankMgr.getActivityRankListModel(activityId, eGamePlayerEventType.PageantTotalScoreRank.getValue());//总分
        if (null != rankListModelAll) {
            int rankingAll = rankListModelAll.unlockGetMyRank(player.getUserId());
            result.setTotalRanking(rankingAll);
        }
    }

    @Override
    public void sendData(GamePlayer player) {
        if (needSend) {
            PageantProto.PageantSettlementBeautyRespMsg.Builder respMsg = PageantProto.PageantSettlementBeautyRespMsg.newBuilder();
            if (0 == ret) {
                respMsg.setRet(result.getRet());
                if (0 == result.getRet()) {
                    if (null != result.getUserPageantData()) {
                        respMsg.setBeautyId(result.getOldBeautyId());
                        respMsg.setScore(result.getScore());
                        respMsg.setEntity(PageantPb.parsePageantBaseRespMsg(result.getUserPageantData()));
                        respMsg.setSingleRanking(result.getSingleRanking());
                        respMsg.setUpRanking(0);
                        respMsg.setTotalRanking(result.getTotalRanking());
                        respMsg.setTotalUpRanking(0);
                        respMsg.setEvaluation(result.getEvaluation());
                    }
                }
            } else {
                respMsg.setRet(ret);
            }
            player.sendPacket(Protocol.U_PAGEANT_SETTLEMENT, respMsg);
        }
    }
}
